﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.components {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.filters.*;
    import net.wooga.pets.events.*;
    import reprise.tweens.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import com.robertpenner.easing.*;
    import flash.ui.*;

    public class CustomizeView extends Sprite {

        private var m_thumbMc:Sprite;
        private var m_snapArea:DisplayObject;
        private var m_dropShadow:BitmapFilter;
        private var m_yellowOutline:BitmapFilter;
        private var m_greenOutline:BitmapFilter;
        private var m_thumbScaleFactor:Number;
        private var m_tweenSnapOut:SimpleTween;
        private var m_tweenSnapIn:SimpleTween;
        private var m_targetRect:Rectangle;

        public static const DEFAULT_SCALE:Number = 0.4;
        private static const OUT_ALPHA:Number = 0.7;
        private static const ASSET_NAME:String = "asset";

        public function CustomizeView(){
            this.m_dropShadow = Filters.getFilter(Filters.SHADOW);
            this.m_yellowOutline = Filters.getFilter(Filters.YELLOW_OUTLINE);
            this.m_greenOutline = Filters.getFilter(Filters.GREEN_OUTLINE);
            mouseChildren = (mouseEnabled = false);
            this.m_thumbMc = new Sprite();
        }
        public function replaceThumb(_arg1:MovieClip):void{
            _arg1.name = ASSET_NAME;
            Displays.removeChildByName(this.m_thumbMc, _arg1.name);
            this.m_thumbMc.addChild(_arg1);
            this.m_snapArea = ((_arg1.getChildByName(GameConstants.SNAP_AREA)) || (this.createSnapArea(_arg1)));
        }
        public function customize(_arg1:MovieClip, _arg2:Number):void{
            Mouse.cursor = MouseCursor.HAND;
            this.replaceThumb(_arg1);
            this.m_thumbScaleFactor = (this.m_thumbMc.scaleX = (this.m_thumbMc.scaleY = _arg2));
            this.m_thumbMc.filters = [this.m_yellowOutline, this.m_dropShadow];
            this.m_thumbMc.alpha = OUT_ALPHA;
            this.m_thumbMc.mouseChildren = (this.m_thumbMc.mouseEnabled = false);
            this.setThumbPos(mouseX, mouseY);
            addChild(this.m_thumbMc);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            stage.addEventListener(MouseEvent.CLICK, this.onClick);
        }
        private function createSnapArea(_arg1:MovieClip):DisplayObject{
            var _local2:Sprite = new Sprite();
            _local2.name = GameConstants.SNAP_AREA;
            _local2.x = (_arg1.width / 2);
            _local2.y = (_arg1.height / 2);
            Drawings.drawRect(_local2.graphics, -(_local2.x), -(_local2.y), _arg1.width, _arg1.height);
            _arg1.addChild(_local2);
            return (_local2);
        }
        public function snapInThumb(_arg1:Rectangle, _arg2:Number):void{
            this.m_targetRect = _arg1;
            this.m_thumbScaleFactor = _arg2;
            this.m_thumbMc.filters = [this.m_greenOutline];
            this.m_thumbMc.alpha = 1;
            if (this.m_tweenSnapOut){
                this.m_tweenSnapOut.cancel();
                this.m_tweenSnapOut = null;
            };
            var _local3:Point = this.calcThumbPos(mouseX, mouseY);
            this.m_tweenSnapIn = this.initTween(this.m_thumbMc, _local3.x, _local3.y, this.m_thumbScaleFactor);
            this.onMouseMove();
        }
        public function snapOutThumb():void{
            this.m_targetRect = null;
            this.m_tweenSnapIn = null;
            this.m_thumbScaleFactor = DEFAULT_SCALE;
            this.m_thumbMc.filters = [this.m_yellowOutline, this.m_dropShadow];
            this.m_thumbMc.alpha = OUT_ALPHA;
            var _local1:Point = this.calcThumbPos(mouseX, mouseY);
            this.m_tweenSnapOut = this.initTween(this.m_thumbMc, _local1.x, _local1.y, this.m_thumbScaleFactor);
            this.onMouseMove();
        }
        private function initTween(_arg1:DisplayObject, _arg2:Number, _arg3:Number, _arg4:Number, _arg5:SimpleTween=null):SimpleTween{
            _arg5 = ((_arg5) || (new SimpleTween(200)));
            _arg5.addTweenProperty(_arg1, "scaleX", _arg1.scaleX, _arg4, Quint.easeOut);
            _arg5.addTweenProperty(_arg1, "scaleY", _arg1.scaleY, _arg4, Quint.easeOut);
            _arg5.addTweenProperty(_arg1, "x", _arg1.x, _arg2, Quint.easeOut);
            _arg5.addTweenProperty(_arg1, "y", _arg1.y, _arg3, Quint.easeOut);
            if (!_arg5.isExecuting()){
                _arg5.execute();
            };
            return (_arg5);
        }
        private function onMouseMove(_arg1:MouseEvent=null):void{
            var _local3:Rectangle;
            var _local4:Rectangle;
            var _local2:Point = new Point(mouseX, mouseY);
            if (this.m_tweenSnapOut){
                if (!this.m_tweenSnapOut.isExecuting()){
                    this.m_tweenSnapOut = null;
                    this.setThumbPos(_local2.x, _local2.y);
                } else {
                    _local2 = this.calcThumbPos(_local2.x, _local2.y);
                    this.m_tweenSnapOut.addTweenProperty(this.m_thumbMc, "x", this.m_thumbMc.x, _local2.x, Quint.easeOut);
                    this.m_tweenSnapOut.addTweenProperty(this.m_thumbMc, "y", this.m_thumbMc.y, _local2.y, Quint.easeOut);
                };
            } else {
                if (this.m_tweenSnapIn){
                    _local3 = this.m_snapArea.getRect(stage);
                    _local3.x = mouseX;
                    _local3.y = mouseY;
                    _local4 = new Rectangle(_local2.x, _local2.y, this.m_thumbMc.width, this.m_thumbMc.height);
                    _local2 = this.calcConstraints(_local3);
                    if (!this.m_tweenSnapIn.isExecuting()){
                        this.setThumbPos(_local2.x, _local2.y);
                    } else {
                        _local2 = this.calcThumbPos(_local2.x, _local2.y);
                        this.m_tweenSnapIn.addTweenProperty(this.m_thumbMc, "x", this.m_thumbMc.x, _local2.x, Quint.easeOut);
                        this.m_tweenSnapIn.addTweenProperty(this.m_thumbMc, "y", this.m_thumbMc.y, _local2.y, Quint.easeOut);
                    };
                } else {
                    this.setThumbPos(_local2.x, _local2.y);
                };
            };
        }
        private function setThumbPos(_arg1:Number, _arg2:Number):void{
            var _local3:Point = this.calcThumbPos(_arg1, _arg2);
            this.m_thumbMc.x = _local3.x;
            this.m_thumbMc.y = _local3.y;
        }
        private function calcThumbPos(_arg1:Number, _arg2:Number):Point{
            _arg1 = (_arg1 - (this.m_snapArea.x * this.m_thumbScaleFactor));
            _arg2 = (_arg2 - (this.m_snapArea.y * this.m_thumbScaleFactor));
            return (new Point(_arg1, _arg2));
        }
        private function calcConstraints(_arg1:Rectangle):Point{
            var _local2:Number = ((this.m_snapArea.width / 2) * this.m_thumbScaleFactor);
            var _local3:Number = ((this.m_snapArea.height / 2) * this.m_thumbScaleFactor);
            var _local4:Number = (this.m_targetRect.left + _local2);
            var _local5:Number = (this.m_targetRect.right - _local2);
            var _local6:Number = (this.m_targetRect.top + _local3);
            var _local7:Number = (this.m_targetRect.bottom - _local3);
            var _local8:Number = this.calcConstraint(_arg1.x, _local4, _local5);
            var _local9:Number = this.calcConstraint(_arg1.y, _local6, _local7);
            return (new Point(_local8, _local9));
        }
        private function calcConstraint(_arg1:Number, _arg2:Number, _arg3:Number):int{
            if (_arg2 >= _arg3){
                return (((_arg2 + _arg3) / 2));
            };
            return (Numbers.constrain(_arg1, _arg2, _arg3));
        }
        private function onClick(_arg1:MouseEvent):void{
            var _local2:Object;
            if (this.m_targetRect){
                _local2 = {x:(this.m_thumbMc.x - this.m_targetRect.left), y:(this.m_thumbMc.y - this.m_targetRect.top)};
                dispatchEvent(new CustomizeViewEvent(CustomizeViewEvent.PLACE_ITEM, null, _local2));
            } else {
                dispatchEvent(new Event(EventConstants.CANCEL));
            };
        }
        public function cancel():void{
            Displays.removeChildByName(this.m_thumbMc, ASSET_NAME);
            this.m_targetRect = null;
            this.m_tweenSnapIn = null;
            this.m_tweenSnapOut = null;
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            stage.removeEventListener(MouseEvent.CLICK, this.onClick);
            Mouse.cursor = MouseCursor.AUTO;
        }

    }
}//package net.wooga.pets.views.components 
